package com.svnservices.hannibal.dao;

import com.svnservices.domaininterface.SvnRepository;
import com.svnservices.hannibal.activerecord.SvnRepositoryAR;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; 
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

import hannibal.dao.db.spring.Dao;

import java.sql.ResultSet;
import java.sql.SQLException;

@javax.annotation.Generated
(
	value="hannibal.generator.Generator",
	comments="",
	date = ""
)
   
public class SvnRepositoryDao
	extends Dao
{
	protected SvnRepositoryDao(javax.sql.DataSource _dataSource)
	{
		super(_dataSource);
	}

	protected ParameterizedRowMapper<SvnRepositoryAR> mapper = new ParameterizedRowMapper<SvnRepositoryAR>()
	{
		public SvnRepositoryAR mapRow(ResultSet _resultSet, int _rowNum)
				throws SQLException
		{
			SvnRepositoryAR svnRepository = new SvnRepositoryAR();

			svnRepository.setName(hannibal.util.DatabaseUtil.getString(_resultSet, "name"));
			svnRepository.setRoot(hannibal.util.DatabaseUtil.getString(_resultSet, "root"));
			svnRepository.setURL(hannibal.util.DatabaseUtil.getString(_resultSet, "URL"));
			svnRepository.setStatus(hannibal.util.DatabaseUtil.getString(_resultSet, "status"));
			svnRepository.setLastModifiedDate(hannibal.util.DatabaseUtil.getString(_resultSet, "lastModifiedDate"));
			svnRepository.setLastAuthor(hannibal.util.DatabaseUtil.getString(_resultSet, "lastAuthor"));
			svnRepository.setRevision(hannibal.util.DatabaseUtil.getString(_resultSet, "revision"));
			
			return svnRepository;
		}
	};

	public java.util.Collection<SvnRepositoryAR> collect(int _offset, int _limit, String _orderBy)
	{
		String sql = "SELECT name AS name, root AS root, URL AS URL, status AS status, lastModifiedDate AS lastModifiedDate, lastAuthor AS lastAuthor, revision AS revision FROM SvnRepository ORDER BY :orderBy LIMIT ? OFFSET ?";
		sql = sql.replaceAll(":orderBy", _orderBy);

		return query(sql, this.mapper, _limit, _offset);
	}

	public int insert(SvnRepositoryAR _svnRepository)
	{
		final String sql = "INSERT INTO SvnRepository ( name, root, URL, status, lastModifiedDate, lastAuthor, revision) VALUES ( ?, ?, ?, ?, ?, ?, ?)";
		
		return update(sql, _svnRepository.getName(), _svnRepository.getRoot(), _svnRepository.getURL(), _svnRepository.getStatus(), _svnRepository.getLastModifiedDate(), _svnRepository.getLastAuthor(), _svnRepository.getRevision());
	}


	public SvnRepository get(final java.lang.String _name)
	{
		final String sql = "SELECT name AS name, root AS root, URL AS URL, status AS status, lastModifiedDate AS lastModifiedDate, lastAuthor AS lastAuthor, revision AS revision FROM SvnRepository WHERE name = ?";

		return queryForObject(sql, this.mapper, _name);
	}

	public SvnRepository get(SvnRepositoryAR _svnRepository)
	{
		return get(_svnRepository.getName());
	}

	public int update(SvnRepositoryAR _svnRepository)
	{
		final String sql = "UPDATE SvnRepository SET root = ?, URL = ?, status = ?, lastModifiedDate = ?, lastAuthor = ?, revision = ? WHERE name = ?";

		return update(sql, _svnRepository.getRoot(), _svnRepository.getURL(), _svnRepository.getStatus(), _svnRepository.getLastModifiedDate(), _svnRepository.getLastAuthor(), _svnRepository.getRevision(), _svnRepository.getName() );
	}

	public int del(final java.lang.String _name)
	{
		return delete(_name);
	}

	public int delete(final java.lang.String _name)
	{
		final String sql = "DELETE FROM SvnRepository WHERE name = ?";

		return update(sql, _name);
	}

	public int del(SvnRepositoryAR _svnRepository)
	{
		return delete(_svnRepository);
	}

	public int delete(SvnRepositoryAR _svnRepository)
	{
		return delete(_svnRepository.getName());
	}


}